package org.ping.Tool;

import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import java.sql.Connection;
 
/**
 * @author PING
 * 数据库链接的类
 */
public class GetData {
	
	private GetData() {}  
    private static final GetData data = new GetData();  
    //静态工厂方法   
    public static GetData getInstance() {  
        return data;
    }  
	//获取一个数据库链接
	public Connection getConnection() {
		Connection con = null;
		try {
			Context ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/data");
			con = ds.getConnection();
		} catch (NamingException e) {
			LogUtil.getLogUtil(this).showlog(e);
		} catch (SQLException e) {
			LogUtil.getLogUtil(this).showlog(e);
		}
		return con;
	}
	//关闭这个数据库链接//返回数据库链接池
	public void close(Connection con) {
		try {
			con.close();
		} catch (SQLException e) {
			LogUtil.getLogUtil(this).showlog(e);
		}
	}
}
